package sraft

// StateSolt is the interface that slots need to implement.
// Each identity deals with the same events. It's just in a different process.
type StateSlot interface{}

// BaseSlot is Base of Matchine.
// General information for each state.
type BaseSlot struct {
	// current matchine state
	state RaftNodeState

	// leader term
	currentTerm int

	// who get the vote
	votedFor int
}

func (bs *BaseSlot) toLeader() *LeaderSlot {
	bs.state = LeaderState
	return &LeaderSlot{
		BaseSlot:   *bs,
		nextIndex:  make([]int, 0),
		matchIndex: make([]int, 0),
	}
}

func (bs *BaseSlot) toFollower() *FollowerSlot {
	bs.state = FollowerState
	return &FollowerSlot{
		BaseSlot: *bs,
	}
}

func (bs *BaseSlot) toCandidate() *CandidateSlot {
	bs.state = CandidateState
	return &CandidateSlot{
		BaseSlot: *bs,
	}
}

// NewMatchine to New RaftMatchine Instance.
func NewMatchine(state RaftNodeState) StateSlot {
	return &BaseSlot{
		state:       state,
		currentTerm: 0,
		votedFor:    -1,
	}
}
